package com.david.jetpack.room

import androidx.room.*
import kotlinx.coroutines.flow.Flow


@Dao
interface UserDao {
    @Query("SELECT * FROM user")
    fun getAll(): List<User>

    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    fun loadAllByIds(userIds: IntArray): Flow<List<User>>

    @Query(
        "SELECT * FROM user WHERE first_name LIKE :first AND " +
                "last_name LIKE :last LIMIT 1"
    )
    fun findByName(first: String, last: String): Flow<User>

    @Query("SELECT * FROM user WHERE first_name LIKE :first")
    fun findByFirstName(first: String): Flow<List<User>?>

    @Update
    fun updateName(vararg users: User)

    @Insert
    fun insertAll(vararg users: User)

    @Delete
    fun delete(user: User)

    @Query("select first_name from user")
    fun getNames(): Flow<List<UserName>>
}

@Entity(tableName = "school")
data class School(
    @PrimaryKey
    @ColumnInfo(name = "id")
    var id: Int,
    @ColumnInfo(name = "name")
    var name: String,
)
@Dao
interface SchoolDao {
    @Query("select * from School")
    fun getAll(): Flow<List<School>>
}

data class UserName(
    @ColumnInfo(name = "first_name")
    var name: String
)

@Entity
data class User(
    @PrimaryKey val uid: Int,
    @ColumnInfo(name = "first_name") val firstName: String?,
    @ColumnInfo(name = "last_name") val lastName: String?
)
